package com.stone.design.practice.mvp.view;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.stone.design.practice.R;
import com.stone.design.practice.mvp.presenter.ILoginPresenter;
import com.stone.design.practice.mvp.presenter.LonginPresenterCompl;

public class SecondActivity extends AppCompatActivity implements View.OnClickListener, ILoginView {

    private EditText etName;
    private EditText etPassword;
    private Button btnSubmit;
    private Button btnClean;
    ILoginPresenter loginPresenter;
    private ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        initView();
    }

    private void initView() {
        etName = findViewById(R.id.et_name);
        etPassword = findViewById(R.id.et_password);
        btnSubmit = findViewById(R.id.btn_submit);
        btnClean = findViewById(R.id.btn_clean);

        progressBar = findViewById(R.id.progress_login);
        btnSubmit.setOnClickListener(this);
        btnClean.setOnClickListener(this);

        loginPresenter = new LonginPresenterCompl(this);
        loginPresenter.setProgressBarVisibility(View.INVISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_submit: {
                loginPresenter.setProgressBarVisibility(View.VISIBLE);
                btnSubmit.setEnabled(false);
                btnClean.setEnabled(false);
                loginPresenter.doLogin(etName.getText().toString(),
                        etPassword.getText().toString());
                break;
            }

            case R.id.btn_clean: {
                loginPresenter.clear();
                break;
            }

            default:
                break;
        }
    }

    @Override
    public void onClearText() {
        etName.setText("");
        etPassword.setText("");
    }

    @Override
    public void onLoginResult(Boolean result, int code) {

        loginPresenter.setProgressBarVisibility(View.INVISIBLE);
        btnSubmit.setEnabled(true);
        btnClean.setEnabled(true);
        if (result){
            Toast.makeText(this,"Login Success",Toast.LENGTH_SHORT).show();
        }
        else
            Toast.makeText(this,"Login Fail, code = " + code,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onSetProgressBarVisibility(int visibility) {
        progressBar.setVisibility(visibility);
    }
}
